package com.yongninger.dao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper
{
	private Context mContext;
	public DBHelper(Context context, String name, CursorFactory factory,int version) 
	{
		super(context, name, factory, version);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) 
	{
		execSchema(db,"forever.sql");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		
	}
	private void execSchema(SQLiteDatabase db, String schemaName)
	{
		BufferedReader in = null;
		try 
		{
			in = new BufferedReader(new InputStreamReader(mContext.getAssets()
					.open("schema"+"/"+schemaName)));
			String line;
			String  buffer = "";
			while ((line = in.readLine()) != null) 
			{
				buffer += line;
				if (line.trim().endsWith(";")) 
				{
					db.execSQL(buffer.replace(";", ""));
					buffer = "";
				}
			}
		} 
		catch (IOException e) 
		{
			System.out.println(e.getMessage());
		} 
		finally 
		{
			try 
			{
				if (in != null)
					in.close(); 
			} 
			catch (IOException e) 
			{}
		}
	}
}
